﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Effects;

namespace TheOliver.Controls.DigitalDisplay
{
    public partial class NumberDisplay : UserControl
    {
        public NumberDisplay()
        {
            InitializeComponent();
        }

        public int DiplayValue
        {
            get { return (int)GetValue(DiplayValueProperty); }
            set { SetValue(DiplayValueProperty, value); }
        }

        public static readonly DependencyProperty DiplayValueProperty =
            DependencyProperty.Register(
                "DiplayValue", 
                typeof(int), 
                typeof(NumberDisplay), 
                new PropertyMetadata(100, OnDisplayValueChanged));

        private static void OnDisplayValueChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            NumberDisplay nd = sender as NumberDisplay;
            nd.SetNewText((int)args.NewValue);
        }

        public int NumberOfCharacters
        {
            get { return (int)GetValue(NumberOfCharactersProperty); }
            set { SetValue(NumberOfCharactersProperty, value); }
        }

        public static readonly DependencyProperty NumberOfCharactersProperty =
            DependencyProperty.Register(
                "NumberOfCharacters", 
                typeof(int), 
                typeof(NumberDisplay),
                new PropertyMetadata(0, OnNumberOfCharactersChanged));

        private double _characterMargin = 4;
        public double CharacterMargin
        {
            get { return _characterMargin; }
            set { _characterMargin = value; }
        }

        private static void OnNumberOfCharactersChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            NumberDisplay nd = sender as NumberDisplay;

            nd.SetNewText((int)args.NewValue);
        }

        public double CharacterWidth
        {
            get { return (double)GetValue(CharacterWidthProperty); }
            set { SetValue(CharacterWidthProperty, value); }
        }

        public static readonly DependencyProperty CharacterWidthProperty =
            DependencyProperty.Register(
                "CharacterWidth", 
                typeof(double), 
                typeof(NumberDisplay),
                new PropertyMetadata(200.0, OnCharacterWidthChanged));

        private static void OnCharacterWidthChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            NumberDisplay nd = sender as NumberDisplay;

            foreach (var c in nd._layoutroot.Children)
            {
                FrameworkElement fe = c as FrameworkElement;
                fe.Width = (double)args.NewValue;
            }
        }

        public double CharacterHeight
        {
            get { return (double)GetValue(CharacterHeightProperty); }
            set { SetValue(CharacterHeightProperty, value); }
        }

        public static readonly DependencyProperty CharacterHeightProperty =
            DependencyProperty.Register(
                "CharacterHeight",
                typeof(double),
                typeof(NumberDisplay),
                new PropertyMetadata(200.0, OnCharacterHeightChanged));

        private static void OnCharacterHeightChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            NumberDisplay nd = sender as NumberDisplay;

            foreach (var c in nd._layoutroot.Children)
            {
                FrameworkElement fe = c as FrameworkElement;
                fe.Height = (double)args.NewValue;
            }
        }


        private void SetNewText(int number)
        {
            string value = number.ToString();

            for (int i = 0; i < value.Length; i++)
            {
                // Does character position exist
                SingleChar sc;

                if (_layoutroot.Children.Count > i)
                {
                    sc = _layoutroot.Children[i] as SingleChar;
                }
                else
                {
                    sc = new SingleChar();
                    sc.Margin = new System.Windows.Thickness(_characterMargin);

                    // Drop Shadow
                    DropShadowEffect dse = new DropShadowEffect();
                    sc.Effect = dse;

                    _layoutroot.Children.Add(sc);
                }
                sc.Character = value.Substring(i, 1);
            }

            // remove characters
            while (_layoutroot.Children.Count > value.Length)
            {
                _layoutroot.Children.RemoveAt(_layoutroot.Children.Count - 1);
            }
        }

    }
}
